let toDecimal = function(x){             // 保留两位小数
    var f = parseFloat(x); 
    if (isNaN(f)) { 
        return; 
    } 
    f = Math.round(x*100)/100; 
    return f; 
}
	
	
let toDecimal2 = function(x){                        // 强制保留2位小数，如：2，会在2后面补上00.即2.00 
    var f = parseFloat(x); 
    if (isNaN(f)) { 
        return false; 
    } 
    var f = Math.round(x*100)/100; 
    var s = f.toString(); 
    var rs = s.indexOf('.'); 
    if (rs < 0) { 
        rs = s.length; 
        s += '.'; 
    } 
    while (s.length <= rs + 2) { 
        s += '0'; 
    } 
    return s; 
} 
     
let fomatFloat = function(num,index){
    var base = Math.pow(10, index)
    return Math.round(num * base) / base;   
} 


//四舍五入 
console.log("保留2位小数：" + toDecimal(3.14159267)); 
console.log("强制保留2位小数：" + toDecimal2(3.14159267)); 
console.log("保留2位小数：" + toDecimal(3.14559267)); 
console.log("强制保留2位小数：" + toDecimal2(3.15159267)); 
console.log("保留2位小数：" + fomatFloat(3.14559267, 2)); 
console.log("保留1位小数：" + fomatFloat(3.15159267, 1)); 
 
//五舍六入 
console.log("保留2位小数：" + 1000.003.toFixed(2)); 
console.log("保留1位小数：" + 1000.08.toFixed(1)); 
console.log("保留1位小数：" + 1000.04.toFixed(1)); 
console.log("保留1位小数：" + 1000.05.toFixed(1)); 
 
//科学计数 
console.log(3.1415.toExponential(2)); 
console.log(3.1455.toExponential(2)); 
console.log(3.1445.toExponential(2)); 
console.log(3.1465.toExponential(2)); 
console.log(3.1665.toExponential(1)); 
//精确到n位，不含n位 
console.log("精确到小数点第2位" + 3.1415.toPrecision(2)); 
console.log("精确到小数点第3位" + 3.1465.toPrecision(3)); 
console.log("精确到小数点第2位" + 3.1415.toPrecision(2)); 
console.log("精确到小数点第2位" + 3.1455.toPrecision(2)); 
console.log("精确到小数点第5位" + 3.141592679287.toPrecision(5)); 